// pages/login/login.js
import {
  request
} from "../../utils/request";
Page({
  phoneLogin() {
    wx.navigateTo({
      url: '/subpkg/phone-login/phone-login',
    })
  },
  async wxLogin() {

    const res = await wx.getUserProfile({
      desc: '为了提供更好的服务',
    }).catch(err => console.log(err))

    // 拒绝的情况处理
    if (!res) return

    // 解构时候，取别名
    const {
      nickName: nickname,
      avatarUrl: avatar
    } = res.userInfo
    console.log(nickname, avatar)


    // 拿code
    const {
      code
    } = await wx.login()
    console.log(code)
    wx.showLoading({
      title: '拼命加载中....',
    })
    // wx.request({
    //   url: 'http://localhost:3000/api/wxlogin',
    //   method: 'POST',
    //   data: {
    //     nickname,
    //     avatar,
    //     code
    //   },
    //   success: res => {
    //     console.log(res)
    //   },
    //   fail: err => {
    //     console.log(err)
    //   },
    //   complete: () => {
    //     wx.hideLoading()
    //   }
    // })
    const res2 = await request({
      url: 'user/wxlogin',
      method: 'POST',
      data: {
        nickname,
        avatar,
        code,

      },
      tip: '微信登陆中...'

    })
    console.log(res2);
    if (res2.status === 0) {
      wx.setStorageSync('tokn', res2.token)
      wx.showToast({
        title: res2.message,
        duration: 1000,
        success: () => {
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/home/home',
            })
          }, 1000)
        }
      })
    }
  }

})